using Microsoft.EntityFrameworkCore;
using RealtimeCollabGame.Domain.Entities;
using RealtimeCollabGame.Domain.ValueObjects;

namespace RealtimeCollabGame.Infrastructure.Persistence;

public class GameContext : DbContext
{
    public GameContext(DbContextOptions<GameContext> options) : base(options)
    {
    }

    public DbSet<Game> Games { get; set; }
    public DbSet<Player> Players { get; set; }
    public DbSet<PaintAction> PaintActions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Game配置
        modelBuilder.Entity<Game>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Code).IsRequired().HasMaxLength(10);
            entity.Property(e => e.Password).HasMaxLength(255);
            entity.Property(e => e.Status).IsRequired();
            entity.Property(e => e.MaxPlayers).IsRequired();
            entity.Property(e => e.GameDuration).IsRequired();
            entity.Property(e => e.CreatedAt).IsRequired();

            entity.HasIndex(e => e.Code).IsUnique();

            // Canvas作为值对象存储
            entity.OwnsOne(e => e.Canvas, canvas =>
            {
                canvas.Property(c => c.Width).HasColumnName("CanvasWidth");
                canvas.Property(c => c.Height).HasColumnName("CanvasHeight");
            });
        });

        // Player配置
        modelBuilder.Entity<Player>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.NickName).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Avatar).HasMaxLength(200);
            entity.Property(e => e.TotalGames).IsRequired();
            entity.Property(e => e.WinCount).IsRequired();
            entity.Property(e => e.IsOnline).IsRequired();
            entity.Property(e => e.IsReady).IsRequired();
            entity.Property(e => e.CreatedAt).IsRequired();

            // 配置PlayerColor值对象
            entity.OwnsOne(e => e.Color, color =>
            {
                color.Property(c => c.Value).HasColumnName("ColorValue");
            });

            entity.HasIndex(e => e.NickName).IsUnique();

            // 配置Game和Player的关系 - 添加级联删除
            entity.HasOne<Game>()
                .WithMany(g => g.Players)
                .HasForeignKey("GameId")
                .OnDelete(DeleteBehavior.Cascade);
        });

        // PaintAction配置
        modelBuilder.Entity<PaintAction>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.GameId).IsRequired();
            entity.Property(e => e.PlayerId).IsRequired();
            entity.Property(e => e.Timestamp).IsRequired();

            // 配置Position值对象
            entity.OwnsOne(e => e.Position, position =>
            {
                position.Property(p => p.X).HasColumnName("PositionX");
                position.Property(p => p.Y).HasColumnName("PositionY");
            });

            // 配置BrushSize值对象
            entity.OwnsOne(e => e.BrushSize, brushSize =>
            {
                brushSize.Property(b => b.Value).HasColumnName("BrushSizeValue");
            });

            // 配置PlayerColor值对象
            entity.OwnsOne(e => e.Color, color =>
            {
                color.Property(c => c.Value).HasColumnName("ColorValue");
            });

            // 配置外键关系
            entity.HasOne<Game>()
                .WithMany(g => g.PaintActions)
                .HasForeignKey(e => e.GameId)
                .OnDelete(DeleteBehavior.Cascade);
        });
    }
}